/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.jctools.actions;

import java.util.Iterator;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipsejcde.core.JCDECore;
import org.eclipsejcde.core.defaults.Defaults;

public class SetPackageAID implements IObjectActionDelegate, IWorkbenchWindowActionDelegate {
	private StructuredSelection currentSelection;

	@Override
	public void run(IAction action) {
		Iterator<?> iter = currentSelection.iterator();
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		try {
			while (iter.hasNext()) {
				Object obj = iter.next();
				if (obj instanceof IPackageFragment) {
					IPackageFragment packFrag = (IPackageFragment) obj;
					String packageAID = Defaults.PACKAGE_AID;
					IStatus status = JCDECore.getPackageAID(packFrag);
					if (status.getSeverity() == IStatus.OK) {
						packageAID = status.getMessage();
					}
					InputDialog inDialog = new InputDialog(shell, "Set Package AID", "Enter package AID for \"" + packFrag.getElementName() + "\" package", packageAID, null);
					while (true) {
						if (inDialog.open() == Window.OK) {
							packageAID = inDialog.getValue();
							status = JCDECore.setPackageAID(packFrag, packageAID);
							if (status.getSeverity() == IStatus.OK) {
								break;
							} else {
								MessageDialog.openError(shell, "Error", status.getMessage());
							}
						} else {
							break;
						}
					}
					inDialog.close();
				}
			}
			ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (Exception e) {
			MessageDialog.openError(shell, "Error", e.getMessage());
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof StructuredSelection) {
			currentSelection = (StructuredSelection) selection;
			Iterator<?> iter = currentSelection.iterator();
			try {
				while (iter.hasNext()) {
					Object obj = iter.next();
					if (obj instanceof IPackageFragment) {
						IPackageFragment packageFragment = (IPackageFragment) obj;
						if (!packageFragment.exists() || !packageFragment.containsJavaResources()) {
							action.setEnabled(false);
						}
					}
				}
			} catch (Exception e) {
				Shell shell = new Shell();
				MessageDialog.openError(shell, "Error", e.getMessage());
			}
		}
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		// TODO Auto-generated method stub

	}
}
